home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 351-375 / disk_351 / pdc / libsrc.lzh / LibSrc / StdLib / system0.asm < prev    next >
Assembly Source File  |  1990-04-19  |  7KB  |  269 lines

  1. *
  2. * System0.asm - invoke system's command processor
  3. *
  4. * Copyright (C) by Ralph Babel, Guru Meditation Network,
  5. * Falkenweg 3, D-6204 Taunusstein, West-Germany
  6. *
  7. * This piece of code may be used as part of any product as
  8. * long as the source code for the complete program can be
  9. * obtained free of charge (except for a small copying fee)
  10. * and this copyright notice is left unchanged.
  11. *
  12. * Bug reports and improved versions appreciated.
  13. *
  14. * 20-Jul-1987 created original version
  15. * 11-Jan-1988 now based on "official" information only
  16. * 9-Apr-1990  modified heavily by Lionel Hummel for PDC release 3.3
  17. *
  18.  
  19.  
  20.         include "exec/types.i"
  21.         include "libraries/dos.i"
  22.         include "libraries/dosextens.i"
  23.  
  24.  
  25. *
  26. * LONG System0(name, seglist, args)
  27. * char *name;
  28. * BPTR seglist;
  29. * char *args;
  30. *
  31.  
  32.         xdef    _System0
  33.         xref    _DOSBase
  34.  
  35. AbsExecBase equ 4
  36.  
  37.         xref    _LVOFindTask
  38.         xref    _LVOAllocMem
  39.         xref    _LVOSetSignal
  40.         xref    _LVOFreeMem
  41.  
  42.         xref    _ReportSL
  43.  
  44. * parameter offsets & stack
  45.  
  46. SAVED_REGS      reg     a2-a6/d2-d7
  47.  
  48. DELTA           equ     11*4
  49.  
  50. ARG_NAME        equ     4+DELTA
  51. ARG_SEGLIST     equ     8+DELTA
  52. ARG_ARGS        equ     12+DELTA
  53.  
  54. * additional return codes
  55.  
  56. NO_CLI  equ     -1
  57. NO_MEM  equ     -2
  58.  
  59. * local constants
  60.  
  61. MAXBSTR equ     255
  62. LF      equ     10
  63.  
  64. * register usage
  65.  
  66. REG_Result      equr    d3
  67. REG_Process     equr    a2      ;may not be A4, see below!
  68. REG_CLI         equr    a3
  69. REG_CIS         equr    a4      ;may not be A3, see below!
  70. REG_PrevStack   equr    a5
  71. REG_SysBase     equr    a6
  72.  
  73. * local stack frame
  74.  
  75.  STRUCTURE      StackFrame,0
  76.         STRUCT  sf_CommandName,MAXBSTR+1        ;BSTR, length byte!
  77.  
  78.         APTR    sf_PrevStack
  79.  
  80.         APTR    sf_SaveReturnAddr
  81.         BPTR    sf_SaveModule
  82.         BPTR    sf_SaveCommandName
  83.  
  84.         APTR    sf_StackBase
  85.         LONG    sf_StackSize
  86.         LONG    sf_PushSize
  87.  
  88.         APTR    sf_Process
  89.         APTR    sf_CLI
  90.         APTR    sf_CIS
  91.  
  92.         BPTR    sf_SCB_Buf
  93.         LONG    sf_SCB_Pos
  94.         LONG    sf_SCB_End
  95.  
  96.         LABEL   sf_SIZEOF
  97.  
  98. * entry point
  99.  
  100. _System0:
  101.         movem.l SAVED_REGS,-(sp)
  102.  
  103.         moveq   #NO_CLI,REG_Result      ;ERROR - not a CLI task
  104.  
  105.         movea.l AbsExecBase,REG_SysBase
  106.         suba.l  a1,a1
  107.         jsr     _LVOFindTask(REG_SysBase)
  108.         movea.l d0,REG_Process
  109.         move.l  pr_CLI(REG_Process),d0
  110.         beq     quit0
  111.  
  112. * build local stack frame & save some values
  113.  
  114.         lsl.l   #2,d0   ;BPTR to machine pointer
  115.         movea.l d0,REG_CLI
  116.  
  117.         movea.l sp,REG_PrevStack        ;save old stack pointer
  118.         move.l  sp,d0
  119.         andi.b  #$fc,d0 ;make SP longword-aligned for BPTRs
  120.         movea.l d0,sp
  121.         suba.w  #sf_SIZEOF,sp
  122.  
  123.         move.l  REG_PrevStack,sf_PrevStack(sp)
  124.         move.l  REG_Process,sf_Process(sp)
  125.         move.l  REG_CLI,sf_CLI(sp)
  126.  
  127.         move.l  pr_ReturnAddr(REG_Process),sf_SaveReturnAddr(sp)
  128.  
  129. * allocate space for stack
  130.  
  131.         moveq   #NO_MEM,REG_Result      ;ERROR - no memory for STACK
  132.  
  133.         move.l  cli_DefaultStack(REG_CLI),d0    ;in longwords for "VEC"
  134.         lsl.l   #2,d0
  135.         move.l  d0,sf_PushSize(sp)
  136.         addq.l  #4,d0   ;one additional longword
  137.         move.l  d0,sf_StackSize(sp)
  138.         moveq   #0,d1   ;intentionally NOT "MEMF_PUBLIC"!
  139.         jsr     _LVOAllocMem(REG_SysBase)
  140.         tst.l   d0
  141.         beq     quit1
  142.  
  143.         move.l  d0,sf_StackBase(sp)     ;save result
  144.  
  145. * save old command pointer, build new BCPL command name
  146.  
  147.         move.l  cli_CommandName(REG_CLI),sf_SaveCommandName(sp)
  148.  
  149.         movea.l ARG_NAME(REG_PrevStack),a0      ;first parameter to "System0()"
  150.         lea     sf_CommandName+1(sp),a1 ;first char location for BSTR
  151.         move.l  a1,d0
  152.         lsr.l   #2,d0   ;will ignore (+1), BPTR as result
  153.         move.l  d0,cli_CommandName(REG_CLI)
  154.  
  155.         move.w  #MAXBSTR,d0
  156.         bra.s   L2
  157.  
  158. L1      move.b  d1,(a1)+
  159. L2      move.b  (a0)+,d1
  160.         dbeq    d0,L1
  161.  
  162.         suba.l  ARG_NAME(REG_PrevStack),a0      ;subtract original source
  163.         move.l  a0,d0
  164.         subq.l  #1,d0   ;terminating null-byte
  165.         move.b  d0,sf_CommandName(sp)   ;to first location in BSTR
  166.  
  167. * save contents of Current Input Stream
  168.  
  169.         move.l  pr_CIS(REG_Process),d0
  170.         lsl.l   #2,d0
  171.         movea.l d0,REG_CIS      ;contains APTR to CIS
  172.         move.l  REG_CIS,sf_CIS(sp)
  173.  
  174.         move.l  fh_Buf(REG_CIS),sf_SCB_Buf(sp)
  175.         move.l  fh_Pos(REG_CIS),sf_SCB_Pos(sp)
  176.         move.l  fh_End(REG_CIS),sf_SCB_End(sp)
  177.  
  178. * setup start/end indices in Stream Control Block
  179.  
  180.         move.l  ARG_ARGS(REG_PrevStack),a0   ; LW aligned by system()
  181.         move.l  a0,d0
  182.         lsr.l   #2,d0   ;make buffer pointer a BPTR
  183.         move.l  d0,fh_Buf(REG_CIS)
  184.  
  185.         move.l  #0,d0
  186.         bra.s   L4
  187.  
  188. L3      addq.l  #1,d0
  189. L4      move.b  (a0)+,d1
  190.         bne     L3
  191.         move.l  d0,fh_End(REG_CIS)
  192.  
  193.         clr.l   fh_Pos(REG_CIS)
  194.  
  195. * misc setup
  196.  
  197.         clr.l   pr_Result2(REG_Process) ;clear secondary result
  198.  
  199.         moveq   #0,d0
  200.         move.l  #SIGBREAKF_CTRL_C,d1
  201.         jsr     _LVOSetSignal(REG_SysBase)
  202.  
  203. * handle seglist and start address
  204.  
  205.         move.l  cli_Module(REG_CLI),sf_SaveModule(sp)
  206.  
  207.         move.l  ARG_SEGLIST(REG_PrevStack),d0   ;second argument to "System0()"
  208.         move.l  d0,cli_Module(REG_CLI)
  209.         lsl.l   #2,d0
  210.         movea.l d0,a3   ;make it a machine pointer in A3
  211.  
  212. * setup processor registers & C-interface
  213.  
  214.         move.l  ARG_ARGS(REG_PrevStack),a0
  215.         move.l  fh_End(REG_CIS),d0
  216.  
  217. * setup processor registers, BCPL-interface, stack & return address for "Exit()"
  218.  
  219.         movea.l sf_StackBase(sp),a1     ;BCPL stack, low end
  220.         move.l  sf_PushSize(sp),d2
  221.         lea     4(a1,d2.l),a4   ;must not destroy REG_Process!
  222.         move.l  sp,-(a4)        ;previous stack frame
  223.         move.l  d2,-(a4)        ;stack size in bytes
  224.         move.l  a4,pr_ReturnAddr(REG_Process)
  225.         movea.l a4,sp
  226.  
  227.         movea.l _DOSBase,a4     ;large data memory model!
  228.         movem.l dl_A2(a4),a2/a5/a6
  229.  
  230. * now call the command at its entry point
  231.  
  232.         jsr     4(a3)   ;code starts one longword behind segment pointer
  233.  
  234.         move.l  d0,REG_Result   ;save return code
  235.  
  236. * get old stackframe & reload old register contents
  237.  
  238.         movea.l 4(sp),sp        ;old stack frame
  239.  
  240.         movea.l sf_Process(sp),a0
  241.         move.l  sf_SaveReturnAddr(sp),pr_ReturnAddr(a0)
  242.  
  243.         movea.l sf_CLI(sp),a0
  244.         move.l  sf_SaveCommandName(sp),cli_CommandName(a0)
  245.         move.l  sf_SaveModule(sp),cli_Module(a0)
  246.  
  247. * restore original contents of Current Input Stream
  248.  
  249.         movea.l sf_CIS(sp),a0
  250.         move.l  sf_SCB_Buf(sp),fh_Buf(a0)
  251.         move.l  sf_SCB_Pos(sp),fh_Pos(a0)
  252.         move.l  sf_SCB_End(sp),fh_End(a0)
  253.  
  254. * free temporary stack
  255.  
  256.         movea.l AbsExecBase,REG_SysBase
  257.         movea.l sf_StackBase(sp),a1
  258.         move.l  sf_StackSize(sp),d0
  259.         jsr     _LVOFreeMem(REG_SysBase)
  260.  
  261. quit1   movea.l sf_PrevStack(sp),sp     ;UNLINK local variables
  262.  
  263. quit0   move.l  REG_Result,d0
  264.  
  265.         movem.l (sp)+,SAVED_REGS
  266.         rts
  267.  
  268.         end
  269.